import { Application } from "egg";
import { ObjectId } from "mongoose";
import * as AutoIncrementFactory from "mongoose-sequence";
export interface DrugFactoryProps {
  name: string; // 药厂名称
  address: string; // 药厂地址
  company_id: ObjectId; // 所属公司
  is_delete: boolean; // 是否删除
}
function initDrugFactoryModel(app: Application) {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const AutoIncrement = AutoIncrementFactory(mongoose);
  const DrugFactoryScheme = new Schema<DrugFactoryProps>(
    {
      name: { type: String, unique: true, required: true },
      address: { type: String, default: "" },
      company_id: {
        type: Schema.Types.ObjectId,
        ref: "Company",
        required: true,
      },
      is_delete: { type: Boolean, default: false },
    },
    {
      timestamps: true,
      toJSON: {
        transform(_doc, ret) {
          delete ret.__v;
        },
      },
    }
  );
  DrugFactoryScheme.plugin(AutoIncrement, {
    inc_field: "id",
    id: "DrugFactory_counter",
  });
  return mongoose.model<DrugFactoryProps>("DrugFactory", DrugFactoryScheme);
}

export default initDrugFactoryModel;
